package ru.spb.osll.myfit.util.parser;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

import ru.spb.osll.myfit.common.util.Pair;
import ru.spb.osll.myfit.util.CollectionUtils;

public class LogrParserTestCase {
  public static final String INFO_TABLE_NAME = "info table";
  public static final String ERROR_TABLE_NAME = "error table";
  
  private  MyLogParser logParser;
  
  private final String[] log = {
      "INFO 1 2",
      "INFO 2 3",
      "INFO 3 4",
      "ERROR 6 7",
      "INFO 4 5",
      "ERROR 7 8"
  };
  
  private final String[][] expectedInfoTable = {
      {"1", "2"},
      {"2", "3"},
      {"3", "4"},
      {"4", "5"}
  };
  
  private final String[][] expectedErrorTable = {
      {"6", "7"},
      {"7", "8"}
  };
  
  @Before
  public void setUp(){
    logParser = new MyLogParser();
    for(String line: log){
      if(line.startsWith("INFO")){
        logParser.infoLine(line);
      }else{
        logParser.errorLine(line);
      }
    }
  }
  
  @Test
  public void testLogParser(){
    List<IResultSection> sections = logParser.getResults();
    assertEquals(2, sections.size());
    
    IResultSection realFirst = sections.get(0);
    assertEquals(INFO_TABLE_NAME, realFirst.getName());
    
    String[][] realInfoTable = (String[][]) realFirst.getRows().toArray(new String[0][0]);
    assertArrayEquals(expectedInfoTable, realInfoTable);
    
    IResultSection realSecond = sections.get(1);
    assertEquals(ERROR_TABLE_NAME, realSecond.getName());
    
    String[][] realErrorTable = (String[][]) realSecond.getRows().toArray(new String[0][0]);
    assertArrayEquals(expectedErrorTable, realErrorTable);
   
  }
  
  @Test
  public void testPribtResultLogParser(){
    List<IResultSection> sections = logParser.getResults();
    
    for(IResultSection section: sections){
        printSection(section);
    }
  }
  
  private void printSection(IResultSection section){
    System.out.println("Section: " + section.getName());
    for(String[] rows: section.getRows()){
      for(String cell: rows){
        System.out.print(cell + " ");
      }
      System.out.println();
    }
    System.out.println();
  }
  
  private class MyLogParser implements ILogParser{

    private List<Pair<String, String>> errorLines = new ArrayList<Pair<String,String>>();
    private List<Pair<String, String>> infoLines = new ArrayList<Pair<String,String>>();
    
    
    @Override
    public void infoLine(String line) {
      String[] numbers = line.split(" ");
      infoLines.add(new Pair<String, String>(numbers[1], numbers[2]));
      
    }

    @Override
    public void errorLine(String line) {
      String[] numbers = line.split(" ");
      errorLines.add(new Pair<String, String>(numbers[1], numbers[2]));
      
    }

    @Override
    public List<IResultSection> getResults() {
      IResultSection infoSection = new ResultSection(INFO_TABLE_NAME);
      
      for(Pair<String, String> line: infoLines){
          infoSection.addRow(line.getFirst(), line.getSecond());
      }
      
      IResultSection errorSection = new ResultSection(ERROR_TABLE_NAME);
      for(Pair<String, String> line: errorLines){
        errorSection.addRow(line.getFirst(), line.getSecond());
      }
      return CollectionUtils.list(infoSection, errorSection);
    }

    @Override
    public void reset() {
      errorLines.clear();
      infoLines.clear(); 
    }
  }
}
